#include "DBManage.h"

CDBManage::CDBManage()
{
    my_ = nullptr;
}

CDBManage::~CDBManage()
{
    mysql_close(my_);
}

bool CDBManage::InitDB()
{
    //初始化，且将空间开辟在堆上
    my_ = mysql_init(NULL);
    if(my_ == NULL){
        std::cout<<"DB Init false"<<std::endl;
        return false;
    }
    //链接
    MYSQL* ret = mysql_real_connect(my_, DBSERVER_IP, DBSERVER_USER, 
                        DBSERVER_PASSWD, DBSERVER_DB, 3306, NULL, 0);
    if(ret == NULL){
        std::cout<<"DB Init connect"<<std::endl;
        return false;
    }

    //设置字符集
    mysql_set_character_set(my_, "utf8");

    return true;
}

bool CDBManage::MysqlQuery(const std::string sql)
{
    int ret = mysql_query(my_, sql.c_str());
    if(ret != 0){
        std::cout<<"query false"<<std::endl;
        return false;
    }
    
    return true;
}

bool CDBManage::GetAllUser(std::unordered_map<int, std::vector<std::string>>& result)
{
    const char* sql = "select * from user;";
    if(MysqlQuery(sql) == false){
        printf("GetAllUser false\n");
        return false;
    }

    MYSQL_RES* res = mysql_store_result(my_);
    long long rows = mysql_num_rows(res);

    for(long long i = 0; i < rows; ++i){
        MYSQL_ROW row = mysql_fetch_row(res);
        std::vector<std::string> tmp;
        tmp.push_back(row[0]);
        tmp.push_back(row[1]);
        tmp.push_back(row[2]);
        tmp.push_back(row[3]);
        result[std::stoi(row[0])] = tmp;
    }

    mysql_free_result(res);

    return true;
}

bool CDBManage::CheckTel(std::string telnum)
{
    char sql[1024] = {0};
    sprintf(sql, "select telnum from user where telnum='%s';", telnum.c_str());
    if(MysqlQuery(sql) == false){
        printf("CheckTel false\n");
        return false;
    }

    MYSQL_RES* res = mysql_store_result(my_);

    int rows = mysql_num_rows(res);

    mysql_free_result(res);

    if(rows == 0)
        return true;
    
    return false;
}

bool CDBManage::RegisterUser(std::string& nickname, std::string& telnum, 
                            std::string& passwd, int pre_id)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into user(user_id, nickname, telnum, passwd) values\
    (%d, '%s', '%s', '%s');", pre_id, nickname.c_str(), telnum.c_str(), passwd.c_str());

    if(MysqlQuery(sql) == false){
        printf("RegisterUser false\n");
        return false;
    }
    
    return true;
}

//返回数据库中好友信息
bool CDBManage::ReturnFriend(int user_id, std::vector<int>& friend_id)
{
    char sql[1024] = {0};
    sprintf(sql, "select friend_id from friend_info where user_id=%d;", user_id);

    if(MysqlQuery(sql) == false){
        printf("ReturnFriend false\n");
        return false;
    }
    
    MYSQL_RES* res = mysql_store_result(my_);

    int rows = mysql_num_rows(res);

    for(int i = 0; i < rows; ++i){
        MYSQL_ROW row = mysql_fetch_row(res);
        friend_id.push_back(std::stoi(row[0]));
    }

    mysql_free_result(res);

    return true;
}

bool CDBManage::RenewFriendInfo(int user_id, int friend_id)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into friend_info values(%d, %d);", user_id, friend_id);

    if(MysqlQuery(sql) == false){
        printf("InsertFriend false\n");
        return false;
    }

    return true;
}
